#include"task.h"
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<unistd.h>

int register_task(struct task_manager *Task_manager, struct task *task)
{
	log_info("begin register_task.");

	if (Task_manager == NULL || task == NULL) {
		log_error("task_manager is NULL or task is NULL.");
		return -1;
	}

	return task_manager_add_task(Task_manager, task);
}


int register_plugin_task(struct task_manager *Task_manager,char *task_name, void *(*func)(void *))
{
	log_info("begin the register task");

	if(Task_manager==NULL || func==NULL)
	{
		log_error("task_manager is NULL or func is NULL");
	}
	
	struct task *task=(struct task *)malloc(sizeof(struct task));

	if(task==NULL)
	{
		log_error("No enough Mem.");
	}	

	if(task_name!=NULL)
	{
		strncpy(task->task_name,task_name,TASK_NAME_MAX_LEN);
	}
	else
	{
		strncpy(task->task_name,"no name",TASK_NAME_MAX_LEN);
	}

	task->task_name[TASK_NAME_MAX_LEN-1]='\0';

	task->is_default=1;
	task->func=func;
	task->next=NULL;
	
	//log_print("-->register user mode task: %s successful.", task_name != NULL ? task_name : "no name");
	log_info("register user mode task");
	
	return register_task(Task_manager,task);
}

void start_task(struct task_manager *Task_manager)
{
	log_info("begin start_default_task.");

	if (Task_manager == NULL) {
		log_error("task_manager is NULL.");
		return; 
	}

	pthread_mutex_lock(&(Task_manager->lock));

	struct task *task = Task_manager->task_head;

	for ( ; task != NULL; task = task->next) {
		if (task->func == NULL) {
			//log_warning("task: %s's func is NULL.", task->task_name);
			log_info("task func is NULL");
			continue;
		}

		int ret;
		ret = pthread_create(&task->tid, NULL, task->func, NULL);	
		if (ret != 0) {
			//log_error("can't start task %s, ret = %d.", task->task_name, ret);
			log_error("can not start task");
			continue;
		}

		if (task->is_default == 1) {
			//log_info("start default task: %s.", task->task_name);
			//log_print("start default task: %s.", task->task_name);
			log_info("start default task");
		} else {
			//log_info("start user mode task: %s.", task->task_name);
			//log_print("start user mode task: %s.", task->task_name);
			log_info("start user mode task");
		}

		/* for test */
		sleep(1);
	}
	pthread_mutex_unlock(&(Task_manager->lock));
}

struct task *get_task_by_name(struct task_manager * Task_manager,char *name)
{

	log_info("begin get_task_by_name.");

	if (Task_manager == NULL || name == NULL) {
		log_error("task_manager is NULL or name is NULL.");
		return NULL;
	}

	/* fixme:task_manager may be NULL now... */
//	pthread_mutex_lock(&(task_manager->lock));
	if (strcmp(name, "no name") == 0)
	{
		log_warning("we cann't get task 'no name'.");
		return NULL;
	}

	struct task *tmp;
	for (tmp = Task_manager->task_head; tmp != NULL && strcmp(tmp->task_name, name) != 0; tmp = tmp->next) {
		/* waitting... */
	}
//	pthread_mutex_unlock(&(task_manager->lock));

	return tmp;
}

struct task *get_task_by_tid(struct task_manager *Task_manager,pthread_t tid)
{
	log_info("begin get_task_by_tid");

	struct task *tmp=Task_manager->task_head;

	while(tmp->tid==tid)
	{
		tmp=tmp->next;
	}

	return tmp;
}


